package com.lxw.nonghe.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author lxw
 * @since 2025-04-17
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    IPage<User> selectUserPage(Page<User> page, @Param("username") String username);

    @Select("WITH months AS (" +
            "    SELECT 1 AS month " +
            "    UNION ALL SELECT 2 " +
            "    UNION ALL SELECT 3 " +
            "    UNION ALL SELECT 4 " +
            "    UNION ALL SELECT 5 " +
            "    UNION ALL SELECT 6 " +
            "    UNION ALL SELECT 7 " +
            "    UNION ALL SELECT 8 " +
            "    UNION ALL SELECT 9 " +
            "    UNION ALL SELECT 10 " +
            "    UNION ALL SELECT 11 " +
            "    UNION ALL SELECT 12 " +
            ") " +
            "SELECT COALESCE(COUNT(tu.user_id), 0) as count " +
            "FROM months m " +
            "LEFT JOIN t_user tu ON MONTH(tu.register_time) = m.month AND YEAR(tu.register_time) = YEAR(CURRENT_DATE) " +
            "GROUP BY m.month " +
            "ORDER BY m.month")
    List<Integer> getRegisterTimes();
}
